﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using SMP_QuanLySV.DAL;

namespace SMP_QuanLySV.BLL
{
    class BLLLop_SV
    {
        List<Lop_SV> list;
        QuanLySVDataContext db;

        public BLLLop_SV()
        {
            this.list = new QuanLySVDataContext().Lop_SVs.ToList();
            this.db = new QuanLySVDataContext();
        }

        public bool checkExist(Lop_SV temp)
        {
            foreach (Lop_SV dt in list)
            {
                if (dt.LopID.Equals(temp.LopID))
                    return true;
            }
            return false;
        }

        // update thong tin Lopsv
        public bool updateLop(Lop_SV update)
        {
            try
            {

                var temp = (from c in db.Lop_SVs
                            where c.LopID == update.LopID
                            select c).First();
                Lop_SV found = (Lop_SV)temp;
                found.LopID = update.LopID;
                found.TenLop = update.TenLop;
                found.NamVaoHoc = update.NamVaoHoc;
                found.GhiChu = update.GhiChu;
                found.DaXetTN = update.DaXetTN;
                found.KhoaSo = update.KhoaSo;

                db.SubmitChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }

        // them Lop moi vao database
        public bool insertLop(Lop_SV temp)
        {
            try
            {
                db.Lop_SVs.InsertOnSubmit(temp);

                db.SubmitChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }

        // xoa lop
        public bool deleteLop(Lop_SV temp)
        {
            try
            {
                var dt = (from c in db.Lop_SVs
                          where c.LopID == temp.LopID
                          select c).First();
                Lop_SV found = (Lop_SV)dt;

                db.Lop_SVs.DeleteOnSubmit(found);

                db.SubmitChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }

        // tra ve danh sach cac Lop
        public List<Lop_SV> returnListLop()
        {
            this.list = new QuanLySVDataContext().Lop_SVs.ToList();
            return list;
        }

        // tra ve 1 Lop theo so thu tu trong list
        public Lop_SV retrieveByIndex(int i)
        {
            this.list = new QuanLySVDataContext().Lop_SVs.ToList();
            if (i >= 0 && i < list.Count)
                return list[i];
            else
                return null;
        }

        // tra ve 1 Lop theo ma Lop
        public Lop_SV retrieveByID(string ID)
        {
            this.list = new QuanLySVDataContext().Lop_SVs.ToList();
            foreach (Lop_SV temp in list)
                if (temp.LopID == ID)
                {
                    return temp;
                }
            return null;
        }

        // kiem tra thong tin 1 Lop co hop le hay khong
        public bool kiemTra(Lop_SV temp)
        {
            if (temp.LopID == "") return false;
            return true;
        }

    }
}
